package com.clayecloud.module_house.viewmodels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_common.apis.HouseApi;
import com.clayecloud.lib_common.apis.entity.house.ClientUserBindHouse;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.MessageUtil;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ActivityBindHouseViewModel extends ViewModel {

    /** 省 */
    private String mProvince = "北京市";
    /** 市 */
    private String mCity = "北京市";
    /** 区 */
    private String mArea = "海淀区";

    private MutableLiveData<Integer> submitSuccess;

    private HouseApi houseApi;

    public String getmProvince() {
        return mProvince;
    }

    public void setmProvince(String mProvince) {
        this.mProvince = mProvince;
    }

    public String getmCity() {
        return mCity;
    }

    public void setmCity(String mCity) {
        this.mCity = mCity;
    }

    public String getmArea() {
        return mArea;
    }

    public void setmArea(String mArea) {
        this.mArea = mArea;
    }

    public MutableLiveData<Integer> getSubmitSuccess() {
        return submitSuccess;
    }

    public void setSubmitSuccess(MutableLiveData<Integer> submitSuccess) {
        this.submitSuccess = submitSuccess;
    }

    /**
     * 处理此模块的数据
     * @return boolean 数据是否处理成功
     */
    public Boolean loadData(){
        houseApi = HttpUtil.getInstance().create(HouseApi.class);
        MutableLiveData<Integer> mutableLiveData = new MutableLiveData<>();
        mutableLiveData.setValue(0);
        setSubmitSuccess(mutableLiveData);
        return true;
    }

    /**
     * 绑定房屋函数
     */
    public void submit(ClientUserBindHouse clientUserBindHouse){
        houseApi.userBindHouse(clientUserBindHouse).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if (messageUtil.getResult()){
                            MutableLiveData<Integer> success = getSubmitSuccess();
                            success.setValue(1);
                            setSubmitSuccess(success);
                        }else {
                            MutableLiveData<Integer> success = getSubmitSuccess();
                            success.setValue(2);
                            setSubmitSuccess(success);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("ActivityBindHouseViewModel => submit => onError",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });




    }



}
